package s47.dqj.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import s47.dqj.bean.DepartmentPerson;

/**
 * 3.科室人员信息表
 * @author Administrator
 *
 */

@Repository("departmentPersonDaoImpl")
public class DepartmentPersonDaoImpl extends HibernateDaoSupport implements DepartmentPersonDaoInf {
	
	/* (non-Javadoc)
	 * @see s47.dqj.dao.DepartmentPersonDaoInf#setSupperSessionFactory(org.hibernate.SessionFactory)
	 */
	@Override
	@Resource(name="sessionFactory")
	public void setSupperSessionFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	/* (non-Javadoc)
	 * @see s47.dqj.dao.DepartmentPersonDaoInf#findAll()
	 */
	@Override
	public List<DepartmentPerson> findAll(){
		String hql="from DepartmentPerson";
		return getHibernateTemplate().find(hql);
	}
	
	/* (non-Javadoc)
	 * @see s47.dqj.dao.DepartmentPersonDaoInf#findAllByDpstate(s47.dqj.bean.DepartmentPerson)
	 */
	@Override
	public List<DepartmentPerson> findAllByDpstate(DepartmentPerson departmentPerson){
		String hql="from DepartmentPerson as dp where dp.dpstate="+departmentPerson.getDpstate();
		return getHibernateTemplate().find(hql);
	}
	
	/* (non-Javadoc)
	 * @see s47.dqj.dao.DepartmentPersonDaoInf#findAllByDtid(s47.dqj.bean.DepartmentPerson)
	 */
	@Override
	public List<DepartmentPerson> findAllByDtid(DepartmentPerson departmentPerson){
		String hql="from DepartmentPerson as dp where dp.dtid.dtid=:dtid";
		Query query =getSession().createQuery(hql);
		query.setInteger("dtid", departmentPerson.getDtid().getDtid());
		return query.list();
	}
	
	/* (non-Javadoc)
	 * @see s47.dqj.dao.DepartmentPersonDaoInf#findObjectByDpnameSimple(s47.dqj.bean.DepartmentPerson)
	 */
	@Override
	public List<DepartmentPerson> findObjectByDpnameSimple(DepartmentPerson departmentPerson){
		String hql="from DepartmentPerson as dp where dp.dpnameSimple=:dpnameSimple";
		Query query =getSession().createQuery(hql);
		query.setString("dpnameSimple",departmentPerson.getDpnameSimple());
		return query.list();
	}
	
	/* (non-Javadoc)
	 * @see s47.dqj.dao.DepartmentPersonDaoInf#findObjectByDpnameSimpleAndDtid(s47.dqj.bean.DepartmentPerson)
	 */
	@Override
	public List<DepartmentPerson> findObjectByDpnameSimpleAndDtid(DepartmentPerson departmentPerson){
		String hql="from DepartmentPerson as dp where dp.dtid.dtid=:dtid and dp.dpnameSimple=:dpnameSimple";
		Query query =getSession().createQuery(hql);
		query.setInteger("dtid", departmentPerson.getDtid().getDtid());
		query.setString("dpnameSimple",departmentPerson.getDpnameSimple());
		return query.list();
	}
	
	/* (non-Javadoc)
	 * @see s47.dqj.dao.DepartmentPersonDaoInf#save(s47.dqj.bean.DepartmentPerson)
	 */
	@Override
	public void save(DepartmentPerson departmentPerson){
		getHibernateTemplate().save(departmentPerson);
	}
	
	/* (non-Javadoc)
	 * @see s47.dqj.dao.DepartmentPersonDaoInf#update(s47.dqj.bean.DepartmentPerson)
	 */
	@Override
	public void update(DepartmentPerson departmentPerson){
		getHibernateTemplate().update(departmentPerson);
	}
	
	public DepartmentPerson findObject(DepartmentPerson departmentPerson){
		return (DepartmentPerson)getHibernateTemplate().load(DepartmentPerson.class, departmentPerson.getDpid());
	}
	
	public static void main(String[] args){
		DepartmentPerson departmentPerson=new DepartmentPerson();
		departmentPerson.setDpid(1);
		System.out.println(departmentPerson.getDpid());
		departmentPerson=new DepartmentPersonDaoImpl().findObject(departmentPerson);
		System.out.println(departmentPerson);
	}
}
